package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.queryParams.KeyProjectQueryParams;
import com.css.fxfzypg.modules.baseData.entity.KeyProjectEntity;
import com.css.fxfzypg.modules.baseData.service.KeyProjectService;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-24 10:23
 *重大工程基础设施数据子模块
 *
 */

@RestController
@RequestMapping("keyProject")
@Slf4j
public class KeyProjectController {


    @Resource
    private KeyProjectService keyProjectService;

    /**
     * 新增
     * @param keyProjectEntity
     * @return
     */
    @PostMapping("/save")
    public RestResponse saveKeyProject(@RequestBody KeyProjectEntity keyProjectEntity){
        RestResponse restResponse=null;
        try {
            keyProjectService.saveKeyProject(keyProjectEntity);
            restResponse=RestResponse.succeed("保存成功");
        } catch (Exception e) {
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/getKeyProjectById")
    public RestResponse getKeyProjectById(String id){
        RestResponse restResponse = null;
        try{
            KeyProjectEntity keyProjectEntity=keyProjectService.getKeyProjectById(id);
            restResponse = RestResponse.succeed(keyProjectEntity);
        }catch (Exception e){
            String errorMessage = "查看失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 更新
     * @param keyProjectEntity
     * @return
     */
    @PostMapping("/update")
    public RestResponse updatKeyProjecte(@RequestBody KeyProjectEntity keyProjectEntity){
        RestResponse response = null;
        try {
            keyProjectService.updateKeyProject(keyProjectEntity);
            response = RestResponse.succeed("修改成功");
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse deleteKeyProject(@PathVariable("ids") List<String> ids){
        try {
            keyProjectService.deleteKeyProject(ids);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }

    /**
     * 分页
     * @param queryParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getAllKeyProject(HttpServletRequest request, KeyProjectQueryParams queryParams){
        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=keyProjectService.getAllKeyProjectByParams(queryParams,curPage,pageSize);
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/importExcel/{dkpKeyType}")
    public RestResponse importExcel(@RequestParam("file")MultipartFile file,@PathVariable("dkpKeyType") String dkpKeyType){
        RestResponse restResponse = null;
        try {
            String result = keyProjectService.importExcel(file,dkpKeyType);
            restResponse = RestResponse.succeed(result);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导出
     * @param queryParams
     * @return
     */
    @PostMapping("/exportExcel")
    public RestResponse exportExcel(HttpServletResponse response, KeyProjectQueryParams queryParams) {
        RestResponse restResponse = null;
        try {
            keyProjectService.exportExcel(response,queryParams);
            restResponse = RestResponse.succeed("导出成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
